package com.example.onlineanswer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.onlineanswer.common.annotation.RequireRole;
import com.example.onlineanswer.common.result.Result;
import com.example.onlineanswer.model.dto.*;
import com.example.onlineanswer.model.vo.UserInfoVO;
import com.example.onlineanswer.model.vo.UserListVO;
import com.example.onlineanswer.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 管理员用户管理控制器
 */
@Api(tags = "管理员用户管理接口")
@RestController
@RequestMapping("/api/admin/users")
@RequiredArgsConstructor
@RequireRole("ADMIN")
public class AdminUserController {

    private final UserService userService;

    /**
     * 分页查询用户列表
     */
    @ApiOperation("分页查询用户列表")
    @GetMapping
    public Result<IPage<UserListVO>> getUserList(UserQueryDTO queryDTO) {
        IPage<UserListVO> page = userService.getUserList(queryDTO);
        return Result.success(page);
    }

    /**
     * 获取用户详情
     */
    @ApiOperation("获取用户详情")
    @GetMapping("/{id}")
    public Result<UserInfoVO> getUserDetail(@PathVariable("id") Long id) {
        UserInfoVO userInfo = userService.getUserInfo(id);
        return Result.success(userInfo);
    }

    /**
     * 获取可用角色列表
     */
    @ApiOperation("获取可用角色列表")
    @GetMapping("/roles")
    public Result<List<String>> getAllowedRoles() {
        List<String> roles = userService.getAllowedRoles();
        return Result.success(roles);
    }

    /**
     * 添加用户
     */
    @ApiOperation("添加用户")
    @PostMapping
    public Result<Boolean> addUser(@Valid @RequestBody UserAddDTO userAddDTO) {
        boolean result = userService.addUser(userAddDTO);
        return Result.success(result);
    }

    /**
     * 更新用户信息
     */
    @ApiOperation("更新用户信息")
    @PutMapping
    public Result<Boolean> updateUser(@Valid @RequestBody UserUpdateDTO userUpdateDTO) {
        boolean result = userService.updateUser(userUpdateDTO);
        return Result.success(result);
    }

    /**
     * 更新用户密码
     */
    @ApiOperation("更新用户密码")
    @PutMapping("/password")
    public Result<Boolean> updateUserPassword(@Valid @RequestBody UserPasswordDTO userPasswordDTO) {
        boolean result = userService.updateUserPassword(userPasswordDTO);
        return Result.success(result);
    }

    /**
     * 更新用户角色
     */
    @ApiOperation("更新用户角色")
    @PutMapping("/role")
    public Result<Boolean> updateUserRole(@Valid @RequestBody UserRoleDTO userRoleDTO) {
        boolean result = userService.updateUserRole(userRoleDTO);
        return Result.success(result);
    }

    /**
     * 更新用户状态
     */
    @ApiOperation("更新用户状态")
    @PutMapping("/status")
    public Result<Boolean> updateUserStatus(@Valid @RequestBody UserStatusDTO userStatusDTO) {
        boolean result = userService.updateUserStatus(userStatusDTO);
        return Result.success(result);
    }

    /**
     * 删除用户
     */
    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteUser(@PathVariable("id") Long id) {
        boolean result = userService.deleteUser(id);
        return Result.success(result);
    }
} 